package com.liuxu.topnews.Manager;

import android.content.Intent;
import android.content.pm.PackageManager;

import com.liuxu.topnews.BuildConfig;
import com.liuxu.topnews.MyApplication;
import com.liuxu.topnews.R;
import com.liuxu.topnews.service.AccessService;
import com.liuxu.topnews.utils.AccessibilityUtils;
import com.liuxu.topnews.utils.RootUtils;
import com.liuxu.topnews.utils.ToastUtils;

import static com.liuxu.topnews.MyApplication.globalapp;
import static com.liuxu.topnews.MyApplication.permission;

public class ActionManager {
    private static ActionManager Instance;
    public static ActionManager getInstance() {
        if (Instance == null) {
            Instance = new ActionManager();
        }
        return Instance;
    }
    public void Home_Up(int time,int speed) throws InterruptedException {
        switch (globalapp.movetype){
            case  0:
                RootUtils.getInstance().swipe_up(time,speed);
                break;
            case  1:
                RootUtils.getInstance().Key_up(time*10);
                break;
            case  2:
                RootUtils.getInstance().Page_up(time*10);
                break;
            case  3:
                RootUtils.getInstance().Event_up(time);
                break;
            default:
                RootUtils.getInstance().swipe_up(time,speed);
                break;
        }
    }
    public void Home_Down(int time,int speed) throws InterruptedException {
        switch (globalapp.movetype){
            case  0:
                RootUtils.getInstance().swipe_down(time,speed);
                break;
            case  1:
                RootUtils.getInstance().Key_down(time*10);
                break;
            case  2:
                RootUtils.getInstance().Page_down(time*10);
                break;
            case  3:
                RootUtils.getInstance().Event_down(time);
                break;
            default:
                RootUtils.getInstance().swipe_down(time,speed);
                break;
        }
    }
    public void News_Up(int time,int speed) throws InterruptedException {
        switch (globalapp.readtype){
            case  0:
                RootUtils.getInstance().swipe_up(time,speed);
                break;
            case  1:
                RootUtils.getInstance().Key_up(time*10);
                break;
            case  2:
                RootUtils.getInstance().Page_up(time*10);
                break;
            case  3:
                RootUtils.getInstance().Event_up(time);
                break;
            default:
                RootUtils.getInstance().swipe_up(time,speed);
                break;
        }
    }
    public void News_Down(int time,int speed) throws InterruptedException {
        switch (globalapp.readtype){
            case  0:
                RootUtils.getInstance().swipe_down(time,speed);
                break;
            case  1:
                RootUtils.getInstance().Key_down(time*10);
                break;
            case  2:
                RootUtils.getInstance().Page_down(time*10);
                break;
            case  3:
                RootUtils.getInstance().Event_down(time);
                break;
            default:
                RootUtils.getInstance().swipe_down(time,speed);
                break;
        }
    }
    public void Back() throws InterruptedException {
        switch (globalapp.readtype){
            case  0:
                RootUtils.getInstance().Back();
                break;
            case  2:
                AccessibilityUtils.getInstance().performBackClick();
                break;
            default:
                RootUtils.getInstance().Back();
                break;
        }
    }
    public void Home() throws InterruptedException {
        switch (globalapp.readtype){
            case  0:
                RootUtils.getInstance().Home();
                break;
            case  2:
                AccessibilityUtils.getInstance().performHomeClick();
                break;
            default:
                RootUtils.getInstance().Home();
                break;
        }
    }
    public boolean ClickText(String text) throws InterruptedException {
        return RootUtils.getInstance().ClickNodes(AccessibilityUtils.getInstance().findNodesByText(text));
    }
    public boolean ClickText(String text,int i) throws InterruptedException {
        return RootUtils.getInstance().ClickNodes(AccessibilityUtils.getInstance().findNodesByText(text),i);
    }
    public boolean ClickId(String id) throws InterruptedException {
        return RootUtils.getInstance().ClickNodes(AccessibilityUtils.getInstance().findNodesById(id));
    }
    public boolean ClickId(String id,int i) throws InterruptedException {
        return RootUtils.getInstance().ClickNodes(AccessibilityUtils.getInstance().findNodesById(id),i);
    }
    public boolean ClickDesc(String text) throws InterruptedException {
        return RootUtils.getInstance().ClickNode(AccessibilityUtils.getInstance().findTextViewByDesc(text));
    }
    public boolean ClickDesc(String id,int i) throws InterruptedException {
        return RootUtils.getInstance().ClickNode(AccessibilityUtils.getInstance().findTextViewByDesc(id));
    }
    public void ReadNews() throws InterruptedException {
        if (permission) {
            switch (globalapp.readtype) {
                case 0:
                    RootUtils.getInstance().readslowbytp(globalapp.uptime, globalapp.quickup, globalapp.quickdown, globalapp.downtime, globalapp.pointx, globalapp.pointy, globalapp.ClickBack);
                    break;
                case 1:
                    RootUtils.getInstance().readslowbykey(globalapp.uptime, globalapp.downtime, globalapp.pointx, globalapp.pointy, globalapp.ClickBack);
                    break;
                case 2:
                    RootUtils.getInstance().readslowbypage(globalapp.uptime, globalapp.downtime, globalapp.pointx, globalapp.pointy, globalapp.ClickBack);
                    break;
                case 3:
                    RootUtils.getInstance().readslowbyevent(globalapp.uptime, globalapp.quickup, globalapp.downtime, globalapp.pointx, globalapp.pointy, globalapp.ClickBack);
                    break;
                default:
                    RootUtils.getInstance().readslowbytp(globalapp.uptime, globalapp.quickup, globalapp.quickdown, globalapp.downtime, globalapp.pointx, globalapp.pointy, globalapp.ClickBack);
                    break;
            }
        }
    }

    public void Close(String packname) throws InterruptedException {
        RootUtils.getInstance().Close(packname);
        Thread.sleep(1000);switch (BuildConfig.version) {
            case "normal":
                RootUtils.getInstance().execShellCmd("input keyevent 82");
                Thread.sleep(1000);
                RootUtils.getInstance().ClickNodes(AccessibilityUtils.findNodesById("com.android.systemui:id/clearButton"));
                RootUtils.getInstance().Click(360,1160);
                break;
            case "hw":
                RootUtils.getInstance().Click(500,1333);
                Thread.sleep(2000);
                RootUtils.getInstance().ClickNodes(AccessibilityUtils.findNodesByText("可用"));
//                RootUtils.getInstance().Click(360,1142);
                Thread.sleep(2000);
                RootUtils.getInstance().swipe(360,1200,360,700,200);
                break;
            case "read":
                RootUtils.getInstance().Click(500,1333);
                Thread.sleep(2000);
                RootUtils.getInstance().ClickNodes(AccessibilityUtils.findNodesByText("可用"));
//                RootUtils.getInstance().Click(360,1142);
                Thread.sleep(2000);
                RootUtils.getInstance().swipe(360,1200,360,700,200);
                break;
            case "sun":
                RootUtils.getInstance().execShellCmd("input keyevent --longpress 3");
                Thread.sleep(2000);
                RootUtils.getInstance().Click(360,1160);
                break;
            default:
                RootUtils.getInstance().execShellCmd("input keyevent 82");
                Thread.sleep(1000);
                RootUtils.getInstance().ClickNodes(AccessibilityUtils.findNodesById("com.android.systemui:id/clearButton"));
                RootUtils.getInstance().Click(360,1160);
                break;
        }
//        BackGroudManager.getInstance().clean();
        Thread.sleep(1000);
        RootUtils.getInstance().Back();
    }

    public void Click(int x, int y) throws InterruptedException {
        RootUtils.getInstance().Click(x,y);
    }

    public void OpenAPP() throws InterruptedException {
        if (permission) {
            if (MyApplication.NowClassName.equals(globalapp.mainname)) {
                ToastUtils.getInstance().shownow("阅读下一篇");
            } else {
                PackageManager var2 = AccessService.getInstance().getPackageManager();
                new Intent();
                Intent var3 = var2.getLaunchIntentForPackage(globalapp.packname);
                AccessService.getInstance().startActivity(var3);
                Thread.sleep(7000);
            }
        }
    }
    public boolean TextExist(String text) throws InterruptedException {
        if (AccessibilityUtils.getInstance().findNodesByText(text).size()>0){
            return true;
        }
        return false;
    }
    public boolean IdExist(String id) throws InterruptedException {
        if (AccessibilityUtils.getInstance().findNodesById(id).size()>0){
            return true;
        }
        return false;
    }
}
